import demo1.HashBuck2;

import java.util.*;

class Imp implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}
class sulotion {
    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String,Integer> map = new HashMap<>();
        for(String word:words) {
            if(map.get(word) == null) {
                map.put(word,1);
            }else {
                int val = map.get(word);
                map.put(word,val+1);
            }
        }

        PriorityQueue<Map.Entry<String,Integer>> minHeap = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o1.getValue().compareTo(o2.getValue()) == 0) {
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue().compareTo(o2.getValue());
            }
        });

        for(Map.Entry<String,Integer> entry:map.entrySet()) {
            if(minHeap.size()<k) {
                minHeap.offer(entry);
            }else {
                Map.Entry<String,Integer> top = minHeap.peek();
                if(entry.getValue().compareTo(top.getValue())<0) {
                    minHeap.poll();
                    minHeap.offer(entry);
                }else if(entry.getValue().compareTo(top.getValue())== 0){
                    if(entry.getKey().compareTo(top.getKey())>0) {
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }
            }
        }

        List<String> ret = new ArrayList<>();

        for(int i=0;i<k;i++) {
            Map.Entry<String,Integer> top = minHeap.poll();
            ret.add(top.getKey());
        }

        Collections.reverse(ret);
        return ret;
    }

}
public class Test {

    public static void main(String[] args) {
        Imp imp = new Imp();
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(imp);
        priorityQueue.offer(10);
        priorityQueue.offer(4);
        priorityQueue.offer(6);

        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
    }
    static class Student {
        public String id;

        public Student(String id) {
            this.id = id;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Student student = (Student) o;
            return Objects.equals(id, student.id);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id);
        }
    }
    public static void main2(String[] args) {
        int[] array = {1,2,3,3,2};
        Map<Integer,Integer> map = new HashMap<>();
        for(Integer x:array) {
            if(map.get(x) == null) {
                map.put(x,1);
            }else {
                int val = map.get(x);
                map.put(x,val+1);
            }
        }
        for(Map.Entry<Integer,Integer> entry:map.entrySet()) {
            System.out.println("key:" + entry.getKey()+ " val:"+ entry.getValue());
        }
    }

    public static void main1(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("abcd",10);
        map.put("hello",2);
        map.put("test",3);

        System.out.println(map.get("abcd"));
        System.out.println(map);

        for(Map.Entry<String,Integer> entry:map.entrySet()) {
            System.out.println("key:"+entry.getKey()+" val:"+entry.getValue() );
        }

        HashMap<Student,Integer> map2 = new HashMap<>();
//        map2.put(new Student(),2);
//        map2.put(new Student(),3);
//        map2.put(null,2);

        HashSet<String> set = new HashSet<>();
        set.add("hello");
        set.add("abcd");
        set.add("abcd");
        System.out.println(set);
    }
}

















